﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using saigonbook.Dal;
using System.Drawing;

namespace saigonbook.Biz
{
    public class BIZ_sach
    {
        public int masach;
        public BIZ_loaisach loai;
        public string tensach;
        public string tacgia;
        public BIZ_nhaxuatban nhaxuatban;
        public int namxuatban;
        public int giabia;
        public int giaban;
        public int sotrang;
        public string hinhanh;
        public DateTime ngaycapnhat;
        public string tomtatnoidung;

        public int Masach
        {
            get { return masach; }
            set { masach = value; }
        }
        public BIZ_loaisach Loai
        {
            get { return loai; }
            set { loai = value; }
        }

        public string Tenloai
        {
            get { return Loai == null ? String.Empty : Loai.Tenloai; }
        }

        public string Tensach
        {
            get { return tensach; }
            set { tensach = value; }
        }
        public string Tacgia
        {
            get { return tacgia; }
            set { tacgia = value; }
        }
        public BIZ_nhaxuatban Nhaxuatban
        {
            get { return nhaxuatban; }
            set { nhaxuatban = value; }
        }

        public string Tennhaxuatban
        {
            get { return Nhaxuatban == null ? String.Empty : Nhaxuatban.Tennhaxuatban; }
        }

        public int Giabia
        {
            get { return giabia; }
            set { giabia = value; }
        }
        public int Giaban
        {
            get { return giaban; }
            set { giaban = value; }
        }
        public int Sotrang
        {
            get { return sotrang; }
            set { sotrang = value; }
        }

        public Image HinhAnh
        {
            get { return Image.FromFile(@"..\\..\\..\\hethongbansach\hethongbansach\" + hinhanh); }
        }

        public string Hinhanh
        {
            get { return hinhanh; }
            set { hinhanh = value; }
        }
        public DateTime Ngaycapnhat
        {
            get { return ngaycapnhat; }
            set { ngaycapnhat = value; }
        }
        public string Tomtatnoidung
        {
            get { return tomtatnoidung; }
            set { tomtatnoidung = value; }
        }
        public BIZ_sach()
        { }
        public BIZ_sach(int masach, BIZ_loaisach loai, string tensach, string tacgia, BIZ_nhaxuatban nhaxuatban, int namxuatban, int giabia, int giaban, int sotrang, string hinhanh, DateTime ngaycapnhat, string tomtatnoidung)
        {
            this.masach = masach;
            this.loai = loai;
            this.tensach = tensach;
            this.tacgia = tacgia;
            this.nhaxuatban = nhaxuatban;
            this.namxuatban = namxuatban;
            this.giabia = giabia;
            this.giaban = giaban;
            this.sotrang = sotrang;
            this.hinhanh = hinhanh;
            this.ngaycapnhat = ngaycapnhat;
            this.tomtatnoidung = tomtatnoidung;
        }

        public static List<BIZ_sach> Select_sach()
        {
            return DAL_sach.Select_sach();
        }

        public static ComboBox Cbb_sach(ComboBox cbb_sach)
        {
            return DAL_sach.cbb_sach(cbb_sach);
        }

        public static ComboBox Cbb_loaisach(ComboBox cbb_loaisach)
        {
            return DAL_sach.cbb_loaisach(cbb_loaisach);
        }

        public static ComboBox Cbb_NXB(ComboBox cbnxb)
        {
            return DAL_sach.cbb_nhaxuatban(cbnxb);
        }

        public static int laymasach()
        {
            int ma = 0;
            List<BIZ_sach> ds = new List<BIZ_sach>();
            ds = DAL_sach.Select_sach();
            ds = (from dh in ds orderby dh.masach descending select dh).Take(1).ToList();
            foreach (BIZ_sach sp in ds)
            {
                ma = sp.masach + 1;
                break;
            }
            return ma;
        }

        public static void Them_sach(BIZ_sach dto)
        {
            DAL_sach.Them_sach(dto);
        }

        public static void Sua_sach(BIZ_sach dto)
        {
            DAL_sach.Sua_sach(dto);
        }

        public static bool kiemtraxoa(BIZ_sach sach)
        {
            List<BIZ_tonkho> dstk = DAL_tonkho.Select_tonkho(sach);
            List<BIZ_chitietphieunhap> dspn = DAL_phieunhap.Select_chitietphieunhap(sach);
            List<BIZ_chitietdonhang> dsdh = DAL_donhang.Select_chitietdonhang(sach);
            if (dstk.Count > 0 || dspn.Count > 0 || dsdh.Count > 0)
                return false;
            return true;
        }

        public static void Xoa_sach(BIZ_sach dto)
        {
            DAL_sach.Xoa_sach(dto);
        }
    }
}
